// pages/my/myWarehouse/myWarehouse.js
const App = getApp();
const utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //tab
    number:0,
    //定义参数
    markId: wx.getStorageSync('markId'),
    sort:0,
    page:0,
    mainstate:0,
    type:2,
    hasMoreData:true,
    list:[],
    noContent:false//数据为空
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  //排序
  changeSelectSptypes:function (){
    var that = this;
    wx.showActionSheet({
      itemList: ['到期时间从远到近', '到期时间从近到远', '入库时间从近到远','入库时间从远到近'],
      success (res) {
        let sort;//排序
        if (res.tapIndex == 0) {
          sort = 10
        }else if (res.tapIndex == 1){
          sort = 11
        }else if (res.tapIndex == 2){
         sort = 0
        }else{
          sort = 1
        }

        that.setData({
          sort:sort,
          page:0,
          list:[]
        })
        that.getDataList();
      },
      fail (res) {
        console.log(res.errMsg)
      }
    })
  },
  //切换现货
  navli:function (e){
    let num = e.currentTarget.dataset.name;
    let mainstate;
    let type;
    console.log(num)
    if (num == 0) {
      mainstate = 0;
      type = 2
    }else if(num == 1){
      mainstate = 1;
      type = 0
    }else if(num == 2){
      mainstate = 1;
      type = 1
    }else if(num == 3){
      mainstate = 2;
      type = 1
    }
    this.setData({
      page:0,
      number:num,
      mainstate:mainstate,
      type:type,
      list:[]
    })
    this.getDataList();
  },
  
  getDataList:function (){
    var that = this;
    let markId = that.data.markId;
    let page = that.data.page;
    let sort = that.data.sort;
    let mainstate = that.data.mainstate;
    let type = that.data.type;
    utils.requestUrl({
      url:'/niu/apiv3/niu_getuserkuv3.php',
      params:{
        page:page,
        markid:wx.getStorageSync('markId'),
        sort:sort,
        mainstate:mainstate,
        type:type
      }
    }).then((res) =>{
      console.log(res)
      var contentListTem = that.data.list;
      if (res.data.datacount == 0 || res.data.datacount == null) {
        that.setData({
          noContent:true
        })
      }else{
        that.setData({
          noContent:false
        })
      }
      if (res.data.datacount > 0) {
        var contentList = res.data.data
        

        if (contentList.length < res.data.pagesize) {
          that.setData({
            list:contentListTem.concat(contentList),
            hasMoreData: false
          })
        }else{
          that.setData({
            list: contentListTem.concat(contentList),
            hasMoreData: true,
            page: page + 1
        })
        }
      }
    })
  },
  goToDetail:function (e){
    let id = e.currentTarget.dataset.id;
    let toysid = e.currentTarget.dataset.toysid;
    let niupoolid = e.currentTarget.dataset.niupoolid;
    let mainstate = e.currentTarget.dataset.mainstate;
    let bukuanstatus = e.currentTarget.dataset.bukuanstatus;
    if (bukuanstatus == 0 && mainstate != 2){
      wx.navigateTo({
        url: '/pages/HPStore/HPStoreDetail/HPStoreDetail?id='+niupoolid+'&type=bk&kuid='+id,
      })
    }else{
      wx.navigateTo({
        url: '/pages/my/myIndexDetail/myIndexDetail?id='+toysid,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(wx.getStorageSync('markId') == ''){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    this.setData({
      page:0,
      list:[]
    })
    this.getDataList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      // console.log('触底了')
      this.getDataList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var userId = wx.getStorageSync('userId')
    return {
      title: '咸蛋分享',
      path: '/pages/my/myWarehouseShare/myWarehouseShare?userId='+userId,
      imageUrl:'https://api.odancool.com/weixinapp/res/img/common/shareImg.png'
    }
  }
})